package com.example.android_wx.fragment;

import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.fragment.app.Fragment;

import com.example.android_wx.R;
import com.example.android_wx.activity.ZtActivity1;
import com.example.android_wx.activity.ZtMainActivity;
import com.example.android_wx.base.App;
import com.example.android_wx.yiqing.FileSavename;
import com.example.android_wx.yiqing.wkActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Random;


public class FavFragment extends Fragment {

    private Button btn_ok, btn_ok1, bt_sj;
    private TextView fx_time, sp_sfysh, tvwkds;
    private AppCompatEditText et_name, et_xueh, et_fdy, et_xb, et_zy, et_bj, et_cxlx, et_lxfs, et_time, et_fdyshsj;
    private Random random = new Random();
    private int i = 0;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.activity_dialog, container, false);
        final TextView textView = root.findViewById(R.id.text_dashboard);


        btn_ok1 = root.findViewById(R.id.btn_ok1);
        btn_ok = root.findViewById(R.id.btn_ok);
        et_name = root.findViewById(R.id.et_name);
        et_xueh = root.findViewById(R.id.et_xueh);
        et_fdy = root.findViewById(R.id.et_fdy);
        et_xb = root.findViewById(R.id.et_xb);
        et_zy = root.findViewById(R.id.et_zy);
        et_bj = root.findViewById(R.id.et_bj);
        et_cxlx = root.findViewById(R.id.et_cxlx);
        et_lxfs = root.findViewById(R.id.et_lxfs);
        et_time = root.findViewById(R.id.et_time);
        tvwkds = root.findViewById(R.id.tvwkds);
        et_fdyshsj = root.findViewById(R.id.et_fdyshsj);
        bt_sj = root.findViewById(R.id.bt_sj);


        getqyt1();
        getqyt();

        String[] ly = {"学校——宁春城", "学校——万达广场", "学校——北海市人民医院", "学校——科技大厦", "学校——万达影城(南洋国际广场店)", "学校——鸣人书画艺苑", "学校——何免诊所", "学校——中国福利彩票(逸安路店)", "学校——华美广场", "学校——新中源大楼", "学校——中国农业银行(北海大道支行)"};


        bt_sj.setOnClickListener(v -> {

            i++;

            if (i == 10) {
                i = 0;
            }

            et_cxlx.setText(ly[i] + "");
        });


        tvwkds.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), wkActivity.class);
            Toast.makeText(getActivity(), "正在打开网课下单平台", Toast.LENGTH_SHORT).show();
            startActivity(intent);

        });


        Map<String, String> userInfo = FileSavename.getUserInfo(getContext());
        if (userInfo != null) {
            et_name.setText(userInfo.get("name"));
            et_xueh.setText(userInfo.get("xuehao"));
            et_fdy.setText(userInfo.get("fdy"));
            et_xb.setText(userInfo.get("xibu"));
            et_zy.setText(userInfo.get("zhuanye"));
            et_bj.setText(userInfo.get("banji"));
        }


        Intent intent = getActivity().getIntent();


        int lxfs = random.nextInt(9999);
        System.out.println(lxfs);
        if (lxfs < 1000) {
            lxfs = random.nextInt(9999);
            System.out.println(lxfs);
            if (lxfs < 1000) {

            }

        }
        et_lxfs.setText("1877550" + lxfs);


        String data = intent.getStringExtra("data");
        String url = "http://www.lefx.top/api/member/getMemberData?token=" + data;

        OkGo.<String>get(url).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

                try {
                    JSONObject jsonObject = new JSONObject(response.body());
//                    String member_nickname[""] = jsonObject.getString("");
//                    et_name.setText(""+member_nickname);


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                ;
            }
        });


        et_time.setOnClickListener(v -> {
            TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(),
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            et_time.setText("" + hourOfDay + ":" + minute);
                        }
                    }, 20, 33, true);
            timePickerDialog.show();
        });


        btn_ok.setOnClickListener(v -> {
            passDate();
            Toast.makeText(getContext(), "制作成功啦，点击日期和内容可以再次修改", Toast.LENGTH_SHORT).show();
        });

        btn_ok1.setOnClickListener(v -> {
            getOK1();
            Toast.makeText(getContext(), "制作成功啦，点击日期和内容可以再次修改", Toast.LENGTH_SHORT).show();
        });

        return root;
    }


    private void getOK1() {

        Intent intent = new Intent(getActivity(), ZtActivity1.class);


        intent.putExtra("name", et_name.getText().toString().trim());
        intent.putExtra("xuehao", et_xueh.getText().toString().trim());
        intent.putExtra("fdy", et_fdy.getText().toString().trim());
        intent.putExtra("xb", et_xb.getText().toString().trim());
        intent.putExtra("zy", et_zy.getText().toString().trim());
        intent.putExtra("bj", et_bj.getText().toString().trim());
        intent.putExtra("cxlx", et_cxlx.getText().toString().trim());
        intent.putExtra("lxfs", et_lxfs.getText().toString().trim());
        intent.putExtra("time", et_time.getText().toString().trim());
        intent.putExtra("fdyshsj", et_fdyshsj.getText().toString().trim());
        preservation();


        startActivity(intent);
    }

    private void preservation() {
        String name = et_name.getText().toString().trim();
        String xuehao = et_xueh.getText().toString().trim();
        String fdy = et_fdy.getText().toString().trim();
        String xb = et_xb.getText().toString().trim();
        String zy = et_zy.getText().toString().trim();
        String bj = et_bj.getText().toString().trim();

        boolean isSaveSuccess = FileSavename.saveUserInfo(getContext(), name, xuehao, xb, zy, bj, fdy);
    }

    private void passDate() {

        Intent intent = new Intent(getActivity(), ZtMainActivity.class);


        intent.putExtra("name", et_name.getText().toString().trim());
        intent.putExtra("xuehao", et_xueh.getText().toString().trim());
        intent.putExtra("fdy", et_fdy.getText().toString().trim());
        intent.putExtra("xb", et_xb.getText().toString().trim());
        intent.putExtra("zy", et_zy.getText().toString().trim());
        intent.putExtra("bj", et_bj.getText().toString().trim());
        intent.putExtra("cxlx", et_cxlx.getText().toString().trim());
        intent.putExtra("lxfs", et_lxfs.getText().toString().trim());
        intent.putExtra("time", et_time.getText().toString().trim());
        intent.putExtra("fdyshsj", et_fdyshsj.getText().toString().trim());

        preservation();
        startActivity(intent);

    }

    private void getqyt() {

        SimpleDateFormat f = new SimpleDateFormat("  yyyy-MM-                                                                                                                                                 dd ");
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd ");
        Date today = new Date();
        System.out.println("今天是:" + f.format(today));
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);// 今天-1天

        Date today1 = new Date();
        System.out.println("今天是:" + f.format(today1));
        Calendar a = Calendar.getInstance();
        a.setTime(today1);
        a.add(Calendar.DAY_OF_MONTH, -2);// 今天-1天

        Date tomorrow1 = a.getTime();
        Date tomorrow = c.getTime();

        int sj222 = random.nextInt(60);
        int sj23 = random.nextInt(60);

        String sqsj = d.format(tomorrow);

        String[] wcsh = new String[]{d.format(tomorrow), d.format(tomorrow1)};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, wcsh);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


//        tv_sj.setText("申请时间：" + d.format(tomorrow) + " 08:" + sj222);
//        tv_sj1.setText(d.format(tomorrow) + " 08:" + sj222);
//        tv_sj2.setText(d.format(tomorrow)  + " 08:" + (sj222-1));
//        tv_fdysj.setText(f.format(tomorrow) + "  09:"+sj23);

    }

    private void getqyt1() {
        SimpleDateFormat f = new SimpleDateFormat("  yyyy-MM-                                                                                                                                                 dd ");
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd ");
        Date today = new Date();
        System.out.println("今天是:" + f.format(today));
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);// 今天+1天

        Date tomorrow = c.getTime();

        int sj222 = random.nextInt(60);
        int sj23 = random.nextInt(60);

        et_fdyshsj.setText(f.format(tomorrow) + "  09:" + sj23);


        int fdyshsj = Integer.valueOf(sj23);

        if (fdyshsj < 9) {

            et_fdyshsj.setText(f.format(tomorrow) + "  09:0" + fdyshsj);

        } else {
            et_fdyshsj.setText(f.format(tomorrow) + "  09:" + fdyshsj);
        }


    }


}

